package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.ParcelApplication;
import com.taysear.parcel.domain.Currency;
import com.taysear.parcel.domain.StationStatus;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.data.validator.DoubleValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

@SuppressWarnings("serial")
public class CountryFieldFactory extends FieldFactory {
	
	ParcelApplication app;
	public final TextField countryId = new TextField("Id");
	final TextField name = new TextField("Name");
	final TextField vat = new TextField("Vat");
	NativeSelect currency = new NativeSelect("Currency");

	public CountryFieldFactory(ParcelApplication app) {
		this.app = app;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f;
		if ("countryId".equals(propertyId)) {
			countryId.setRequired(true);
			countryId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return countryId;
		} if ("name".equals(propertyId)) {
			name.setRequired(true);
			name.setWidth(Constants.COMMON_FIELD_WIDTH);
			name.setRequiredError("Name is required");
			name.setNullRepresentation("");
			return name;
		} if ("vat".equals(propertyId)) {
			vat.setRequired(true);
			vat.setWidth(Constants.COMMON_FIELD_WIDTH);
			vat.setRequiredError("VAT is required");
			vat.addValidator(new PercentageValidator("Not a valid vat, should be between 0.99 and 0.01"));
			vat.setNullRepresentation("");
			return vat;
		} if ("currency".equals(propertyId)) {
			try{
				currency = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Currency> container = JPAContainerFactory.make(Currency.class, app.PERSISTENCE_UNIT);
				currency.setContainerDataSource(container);
			}
			currency.setWidth(Constants.COMMON_FIELD_WIDTH);
			currency.setNullSelectionAllowed(false);
			currency.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			currency.setRequired(true);
			currency.setRequiredError("Currency is required");
			return currency;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
	
	private class PercentageValidator extends DoubleValidator {
		
		public PercentageValidator(String errorMessage) {
			super(errorMessage);
		}

		@Override
		public boolean isValid(Object value) {
			Boolean isValid = super.isValid(value);
			if(isValid){
				double v = Double.parseDouble(value.toString());
				if(v <= 0.99 && v >= 0.01)
					isValid = true;
				else
					isValid = false;
			}
			return isValid;
		}
		
	}
}
